﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace WindowsFormsApplication1.FiltryJG
{
    class Gaus
    {
        private double[,] Macierz;
        public Bitmap bitmapFiltr { get; set; }
        private int MacierzWidthIHight,width,height;
        private Bitmapp bitmap;
        private double DzielnikNormalizowalny;
        private double sigma;
        private static double M_PI = 3.1415;

        public Gaus(int MacierzWidthIHight,int width,int height,Bitmapp bitmap)
        {
            this.width = width; this.height = height; this.bitmap = bitmap; this.MacierzWidthIHight = MacierzWidthIHight;
            double midCalkulation;
            int odleglosc;
            if (MacierzWidthIHight == 3)
            {
                sigma = 1;
                odleglosc = 1;
            }
            else if (MacierzWidthIHight == 5)
            {
                odleglosc = 2;
                this.sigma = 1.5;
            }
            else 
            {
                odleglosc = 3;
                this.sigma = 2;
            }

             Macierz = new double[MacierzWidthIHight, MacierzWidthIHight];
             for (int row = -odleglosc; row <= odleglosc; row++)
                {
                    for (int col = -odleglosc; col <= odleglosc; col++)
                    {
                        midCalkulation = -((Math.Pow(col, 2) + Math.Pow(row, 2)) / (2 * Math.Pow(sigma, 2)));
                        Macierz[row + odleglosc, col + odleglosc] = (1 / (2 * M_PI * Math.Pow(sigma, 2))) * Math.Exp(midCalkulation);

                        this.DzielnikNormalizowalny += Macierz[row + odleglosc, col + odleglosc];
                    }
                }
         }


        public Bitmap ZwrocObrazekRozmyty()
        {
            bitmapFiltr = new Bitmap(this.width, this.height);
             Double wynikR = 0, wynikG = 0, wynikB = 0;

            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    if (j <= MacierzWidthIHight / 2 || j > this.width - MacierzWidthIHight / 2 - 1 || i <= MacierzWidthIHight / 2 || i > this.height - MacierzWidthIHight / 2 - 1)
                    {
                        bitmapFiltr.SetPixel(j, i,bitmap.GetPixel(j, i));
                    }
                    else
                    {
                        int xPixel = j - MacierzWidthIHight / 2, yPixel = i - MacierzWidthIHight / 2;
                        for (int ii = 0; ii < MacierzWidthIHight; ii++)
                        {
                            for (int jj = 0; jj < MacierzWidthIHight; jj++)
                            {
                                wynikR = wynikR + Macierz[jj, ii] * Convert.ToDouble(bitmap.GetPixelR(xPixel, yPixel));
                                wynikB = wynikB + Macierz[jj, ii] * Convert.ToDouble(bitmap.GetPixelB(xPixel, yPixel));
                                wynikG = wynikG + Macierz[jj, ii] * Convert.ToDouble(bitmap.GetPixelG(xPixel, yPixel));
                                xPixel++;
                            }
                            yPixel++; xPixel = j - MacierzWidthIHight / 2;
                        }
                        wynikR = wynikR / DzielnikNormalizowalny;
                        wynikB = wynikB / DzielnikNormalizowalny;
                        wynikG = wynikG / DzielnikNormalizowalny;
                        bitmapFiltr.SetPixel(j, i, Color.FromArgb((int)wynikR, (int)wynikG, (int)wynikB));
                        wynikR = 0; wynikG = 0; wynikB = 0;
                    }
                }
            }
            // a teraz krawedzie
            int i1 = MacierzWidthIHight / 2+1, i2 = this.height - MacierzWidthIHight / 2 - 1;
            int j1 = MacierzWidthIHight / 2 + 1, j2 = this.width - MacierzWidthIHight / 2 - 1;
            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {

                    if (i <= MacierzWidthIHight / 2 )
                    {
                        bitmapFiltr.SetPixel(j, i, bitmapFiltr.GetPixel(j, i1));
                    }
                    if (i > this.height - MacierzWidthIHight / 2 - 1 )
                    {
                        bitmapFiltr.SetPixel(j, i, bitmapFiltr.GetPixel(j, i2));
                    }
                    if (j <= MacierzWidthIHight / 2 )
                    {
                        bitmapFiltr.SetPixel(j, i, bitmapFiltr.GetPixel(j1, i));
                    }
                    if (j > this.width - MacierzWidthIHight / 2 - 1 )
                    {
                        bitmapFiltr.SetPixel(j, i, bitmapFiltr.GetPixel(j2, i));
                    }
                }
            }

            return this.bitmapFiltr;
        }
    }
}
